package com.bz.zhrc.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
@Slf4j
public class SpringAsyncConfig {

    @Bean
    public AsyncTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //线程命名
        executor.setThreadNamePrefix("pushTaskExecutor");
        //线程池最小个数
        executor.setCorePoolSize(10);
        //线程池最大个数
        executor.setMaxPoolSize(20);
        //线程活动时间
        executor.setKeepAliveSeconds(3);
        //队列容量
        executor.setQueueCapacity(10000);

        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
